import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class PPTitleAndDescHeadView extends StatefulWidget {

    PPTitleAndDescHeadView(String title, { Key key, this.desc = '', this.redirectUrl = '', this.onPressAction } ): this.title = title, super(key: key);

    final String title;
    final String desc;
    final String redirectUrl;
    final Function onPressAction;

    _PPTitleAndDescHeadViewState createState() => _PPTitleAndDescHeadViewState(); 
}

class _PPTitleAndDescHeadViewState extends State<PPTitleAndDescHeadView> {

    String title;
    String desc;
    String redirectUrl;

    @override 
    void initState() {
        title = widget.title;
        desc = widget.desc;
        redirectUrl = widget.redirectUrl;
        super.initState();
    }

    @override 
    void didUpdateWidget(PPTitleAndDescHeadView oldWidget) {
        title = widget.title;
        desc = widget.desc;
        redirectUrl = widget.redirectUrl;
        super.didUpdateWidget(oldWidget);
  }

    @override
    Widget build(BuildContext context) {
        return (Container(
            child: FlatButton(
                padding: EdgeInsets.fromLTRB(0, 0, 0, 5),
                child: Container(
                    height: 44,
                    padding: EdgeInsets.fromLTRB(15, 20, 15, 0),
                    child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                            Text(title, style: TextStyle(fontSize: 17, color: Color.fromRGBO(51, 51, 51, 1))),
                           _getRightWidget()
                        ],
                    ),
                ),
                onPressed: _onPressTitleHead
            ),
        ));
    }

    _getRightWidget() {
        if(redirectUrl != null && redirectUrl.length > 0) {
            return ( Row(
                children: [
                    Text(desc, style: TextStyle(fontSize: 14, color: Color.fromRGBO(153, 153, 153, 1))),
                    Container(
                        margin: EdgeInsets.only(left: 5),
                        child: Image.asset("resource/images/next_gray_bg_arrow_icon.png", width: 14, height: 14, fit: BoxFit.fill),
                    )
                ],
            ));
        } else {
            return null;
        }
    }

    _onPressTitleHead() {
        if(widget.onPressAction != null) {
            widget.onPressAction(title, redirectUrl);
        }
    }

}